package com.xzq.imageviewertest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.github.chrisbanes.photoview.PhotoView;

import java.util.ArrayList;
import java.util.List;

public class ImageGalleryActivity extends AppCompatActivity {

    private ArrayList<CharSequence> imageUrls = new ArrayList<>();

    private int firstIndex;

    private ViewPager viewPager;

    private PagerViewAdapter adapter;

    private List<View> viewList = new ArrayList<>();

    private LinearLayout indicatorView;

    private int currentIndicatorPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= 21) {
            Window window = getWindow();
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.setStatusBarColor(Color.TRANSPARENT);
        }

        setContentView(R.layout.activity_image_gallery);

        Bundle bundle = this.getIntent().getExtras();
        imageUrls = (ArrayList<CharSequence>) bundle.get("imageList");
        firstIndex = (int) bundle.get("position");

        currentIndicatorPosition = firstIndex;

        viewPager = findViewById(R.id.images_view_pager);

        LayoutInflater inflater = LayoutInflater.from(this);

        for (int i = 0; i < imageUrls.size(); i++) {
            String url = (String) imageUrls.get(i);
            View view = inflater.inflate(R.layout.images_view_pager_view, null);
            PhotoView photoView = view.findViewById(R.id.slide_image_view);
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                    overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
                }
            });
            Glide.with(this).load(url).into(photoView);
            viewList.add(view);
        }

        adapter = new PagerViewAdapter(viewList);
        viewPager.setAdapter(adapter);

        viewPager.setCurrentItem(firstIndex);

        indicatorView = findViewById(R.id.linear);


        for (int i = 0; i < imageUrls.size(); i++) {
            View view = new View(this);
            view.setBackgroundResource(R.drawable.indicator_selector_bg);
            view.setEnabled(false);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(18, 18);
            if (i != (imageUrls.size() - 1)) {
                params.rightMargin = 20;
            }
            indicatorView.addView(view, params);
        }

        indicatorView.getChildAt(firstIndex).setEnabled(true);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                indicatorView.getChildAt(currentIndicatorPosition).setEnabled(false);
                indicatorView.getChildAt(position).setEnabled(true);
                currentIndicatorPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.fade_out, R.anim.fade_in);
    }
}